package oad.Mytest;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager.LayoutParams;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ViewFlipper;

public class AndroidTestActivity extends Activity {
	
	private ViewFlipper flipper;
	private Button previous, next;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        initiaView();
        
        flipper.addView(addButtonByText("Start"),	new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        flipper.addView(addTextByText("Gutouwang"));
        flipper.addView(addImageById(R.drawable.bk1));
        flipper.addView(addTextByText("Fushan Liaoli"));
        flipper.addView(addImageById(R.drawable.bk2));
        flipper.addView(addButtonByText("End"), 
        		new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

    }
    
    private void initiaView(){
    	previous = (Button) findViewById(R.id.btnPrev);
    	next = (Button) findViewById(R.id.btnNext);
    	
    	flipper = (ViewFlipper) findViewById(R.id.viewFlipper1);
    	flipper.setInAnimation(AnimationUtils.loadAnimation(this,
                android.R.anim.fade_in));
        flipper.setOutAnimation(AnimationUtils.loadAnimation(this,
                android.R.anim.fade_out));
        
        previous.setOnClickListener(listener);
        next.setOnClickListener(listener);
    }
    
    private OnClickListener listener = new OnClickListener(){
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch(v.getId()){
			case R.id.btnPrev:
				flipper.showPrevious();
				break;
				
			case R.id.btnNext:
				flipper.showNext();
				break;
			}
		}
    };


    
    public View addTextByText(String text){
		TextView tv = new TextView(this);
		tv.setText(text);
		tv.setGravity(1);
		return tv;
    }

	public View addImageById(int id){
		ImageView iv = new ImageView(this);
		iv.setImageResource(id);
		
		return iv;
	}
	
	public View addButtonByText(String text){
		Button btn = new Button(this);
		btn.setText(text);
		return btn;
	}

}